<?php

namespace App\Admin\Controllers;

use App\Models\BookHotSearchWord;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BookHotSearchWordsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('热门搜索词列表');
            //$content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Detail');
            $content->description('description');

            $content->body(Admin::show(BookHotSearchWord::findOrFail($id), function (Show $show) {

                $show->id();

                $show->created_at();
                $show->updated_at();
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改热门搜索词');
            //$content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    public function update($id)
    {
        return $this->form()->update($id);
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加热门搜索词');
            //$content->description('description');

            $content->body($this->form());
        });
    }

    public function store()
    {
        return $this->form()->store();
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(BookHotSearchWord::class, function (Grid $grid) {
            $grid->model()->orderBy('sort', 'asc');

            //$grid->id('ID');
            $grid->word('热门搜索词');
            $grid->sort('排序值');

            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableFilter();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(BookHotSearchWord::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('word', '热门搜索词')->rules('required');
            $form->text('sort', '排序值')->rules('required|numeric')->default(0)->help('值越小越靠前');

            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });
        });
    }
}
